package hu.home.arb.bet365;

import hu.home.arb.dao.Dao;
import hu.home.arb.entity.Html;
import hu.home.arb.entity.Parse;
import hu.home.arb.net.Net;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class Bet365 {
	static Logger logger = LogManager.getLogger(Bet365.class.getName());
	static Connection conn;
	
	public Bet365() throws SQLException {
		super();
		conn = Dao.getConnection();
	}

	public void start() {
		Parse parse = new Parse();
		parse.setBookieId(Bet365Constants.BET365_BOOKIE_ID);
		
	}
	
	public void readPage() throws FailingHttpStatusCodeException, MalformedURLException, IOException, SQLException, InterruptedException {
		logger.info("Start");
		
		WebClient webClient = Net.getWebClient();
		
		//Fő oldal beolvasása
		HtmlPage mainPage = webClient.getPage(Bet365Constants.BET365_MAIN_URL);
		
		//football link megkeresése
		HtmlAnchor footballAnchor =  (HtmlAnchor) (mainPage.getHtmlElementsByAccessKey('3')).get(0);
		HtmlPage footballPage = footballAnchor.click();
		
		//linkek megkeresése
		List<HtmlAnchor> menuLinks = footballPage.getHtmlElementById("splash").getHtmlElementsByTagName("a");
		
		List<String> markets = Arrays.asList("Full Time Result", "Goals Over/Under", "Double Chance");
				
		for(int i = 0; i < menuLinks.size(); i++) {
			HtmlAnchor menuLink = menuLinks.get(i);
			if(markets.contains(menuLink.getTextContent())) {
				//logger.info(menuLink.asXml());
				HtmlPage subPage = menuLink.click();
				webClient.waitForBackgroundJavaScriptStartingBefore(1000);
				if(subPage.getByXPath("//*[@id='subMenu_2']") != null && subPage.getByXPath("//*[@id='subMenu_2']").size() != 0) {
					List<HtmlAnchor> subPageLinks = subPage.getHtmlElementById("subMenu_2").getHtmlElementsByTagName("a");
					for(int j = 0; j < subPageLinks.size(); j++) {
						//logger.info(subPageLinks.get(j).asXml());
						HtmlPage subPage2 = subPageLinks.get(j).click();
						webClient.waitForBackgroundJavaScriptStartingBefore(1000);
						if(subPage2.getByXPath("//*[@id='subMenu_2']") != null && subPage2.getByXPath("//*[@id='subMenu_2']").size() != 0) {
							List<HtmlAnchor> subPage2Links = subPage2.getHtmlElementById("subMenu_2").getHtmlElementsByTagName("a");
							for(int k = 0; k < subPage2Links.size(); k++) {
								//logger.info(subPage2Links.get(k).asXml());
								HtmlPage subPage3 = subPage2Links.get(k).click();
								webClient.waitForBackgroundJavaScriptStartingBefore(1000);
								
								savePage(subPage3);	
							}
						} else if(subPage2.getByXPath("//*[@class='Coupon']") != null && subPage.getByXPath("//*[@class='Coupon']").size() != 0) {
							savePage(subPage2);
						}
					}
				}
			}
			//logger.info(menuLink.getTextContent());
		}		
		//logger.info(menuLinks.toString());
	}
	
	
	public void savePage(HtmlPage page) throws SQLException, InterruptedException {
		//html oldal mentése
		Html html = new Html();
		html.setBookieId(Bet365Constants.BET365_BOOKIE_ID);
		html.setSportId(1);
		html.setText(page.getHtmlElementById("page").asXml());
		
		String sql = "insert into html (html_bookie_id, html_sport_id, html_text) values (?, ?, ?)";
		PreparedStatement pstm = conn.prepareStatement(sql);
		pstm.setInt(1, (int) html.getBookieId());
		pstm.setInt(2, (int) html.getSportId());
		pstm.setString(3, html.getText());
		pstm.executeUpdate();
		logger.info("HTML inserted");
		//logger.info("**************************");
		Thread.currentThread();
		Thread.sleep(4000);
	}
}
